package com.iplant.wms.serviceImpl.dao.wms;

import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.wms.service.po.wms.WMSStockSafe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public class WMSStockSafeDAO extends BaseDAO {

	private static WMSStockSafeDAO Instance = null;
	private static final Logger logger = LoggerFactory.getLogger(WMSStockSafeDAO.class);

	private WMSStockSafeDAO() {
		super();

		setTableName("wms_stocksafe");
	}

	public static WMSStockSafeDAO getInstance() {
		if (Instance == null)
			Instance = new WMSStockSafeDAO();
		return Instance;
	}

	public void WMS_UpdateStockSafe(BMSEmployee wLoginUser, WMSStockSafe wStockSafe, OutResult<Integer> wErrorCode) {
		wErrorCode.set(0);

		try {
			if (wStockSafe == null || wStockSafe.StoreID <= 0 || wStockSafe.MaterialID <= 0
					|| wStockSafe.SafeNum < 0) {
				wErrorCode.set(ExceptionTypes.Parameter.getValue());
				return;
			}
			WMSStockSafe wStockSafeO = this.WMS_CheckStockSafe(wLoginUser, wStockSafe, wErrorCode);
			if (wStockSafeO.ID > 0) {
				wErrorCode.set(ExceptionTypes.Duplication.getValue());
			}
			if (wErrorCode.Result != 0)
				return;

			Map<String, Object> wParwms = new HashMap<>();

			wParwms.put("StoreID", wStockSafe.StoreID);
			wParwms.put("StoreAreaID", wStockSafe.StoreAreaID);
			wParwms.put("MaterialID", wStockSafe.MaterialID);
			wParwms.put("SafeNum", wStockSafe.SafeNum);
			wParwms.put("MinNum", wStockSafe.MinNum);
			wParwms.put("MaxNum", wStockSafe.MaxNum);
			wParwms.put("MaxDeadDate", wStockSafe.MaxDeadDate);
			wParwms.put("DeadDate", wStockSafe.DeadDate);
			wParwms.put("SafeDate", wStockSafe.SafeDate);
			wParwms.put("SafeAlarmDate", wStockSafe.SafeAlarmDate);
			wParwms.put("Description", wStockSafe.Description);

			wParwms.put("EditorID", wLoginUser.getID());
			wParwms.put("EditTime", Calendar.getInstance());
			wParwms.put("Active", wStockSafe.Active);

			if (wStockSafe.ID > 0) {
				wParwms.put("ID", wStockSafe.ID);
				super.Update(super.getTableName("wms_stocksafe"), "ID", wParwms);
			} else {
				wParwms.put("CreatorID", wLoginUser.getID());
				wParwms.put("CreateTime", Calendar.getInstance());
				wStockSafe.ID = super.Insert(super.getTableName("wms_stocksafe"), wParwms);
			}

		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("WMS_UpdateStockSafe", ex);
		}
	}

	public void WMS_ActiveStockSafe(BMSEmployee wLoginUser, WMSStockSafe wStockSafe, int wActive,
			OutResult<Integer> wErrorCode) {
		wErrorCode.set(0);
		// 判断客户信息是否存在(中国：统一社会信用代码，国外:提醒是否有重复）

		try {
			if (wStockSafe == null || wStockSafe.ID <= 0) {
				wErrorCode.set(ExceptionTypes.Parameter.getValue());
				return;
			}

			Map<String, Object> wParwms = new HashMap<>();

			wParwms.put("ID", wStockSafe.ID);
			wParwms.put("EditorID", wLoginUser.getID());
			wParwms.put("EditTime", Calendar.getInstance());
			wParwms.put("Active", wActive);
			super.Update(super.getTableName("wms_stocksafe"), "ID", wParwms);

		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());

			logger.error("WMS_ActiveStockSafe", ex);
		}
	}

	public List<WMSStockSafe> WMS_QueryStockSafeList(BMSEmployee wLoginUser, int wFactoryID, int wStoreRegionID,
			int wStoreID, String wStoreCode, String wStoreName, int wStoreAreaID, String wStoreAreaCode,
			String wStoreAreaName, int wMaterialID, String wMaterialCode, String wMaterialName, String wName,
			int wActive, Pagination wPagination, OutResult<Integer> wErrorCode) {
		return this.WMS_QueryStockSafeList(wLoginUser, -1, "", wFactoryID, wStoreRegionID, wStoreID, wStoreCode,
				wStoreName, wStoreAreaID, wStoreAreaCode, wStoreAreaName, wMaterialID, wMaterialCode, wMaterialName,
				wName, wActive, wPagination, wErrorCode);
	}

	private List<WMSStockSafe> WMS_QueryStockSafeList(BMSEmployee wLoginUser, int wID, String wCode, int wFactoryID,
			int wStoreRegionID, int wStoreID, String wStoreCode, String wStoreName, int wStoreAreaID,
			String wStoreAreaCode, String wStoreAreaName, int wMaterialID, String wMaterialCode, String wMaterialName,
			String wName, int wActive, Pagination wPagination, OutResult<Integer> wErrorCode) {
		List<WMSStockSafe> wWMSStockSafeList = new ArrayList<WMSStockSafe>();
		wErrorCode.set(0);

		try {

			// Step02:查询
			Map<String, Object> wParwms = new HashMap<>();
			String wSQLText = StringUtils.Format("Select t.*,t1.Name as StoreName,t1.Code as StoreCode,t1.FactoryID,"
					+ " t1.StoreRegionID,t5.Name as FactoryName,t5.Code as FactoryCode,t8.Name as StoreAreaName,t8.Code as StoreAreaCode,"
					+ " t2.Name as StoreRegionName,t2.Code as StoreRegionCode,t6.MaterialName,"
					+ " t6.Code as MaterialCode,t6.CYUnitID as UnitID, t7.Code as UnitCode,t7.Name as UnitText, "
					+ " t3.Name as CreatorName ,t4.Name as EditorName  from {0}.wms_stocksafe t  "
					+ " left join {0}.wms_store t1 on t.StoreID=t1.ID "
					+ " left join {0}.wms_storearea t8 on t.StoreAreaID=t8.ID "
					+ " left join {0}.fmc_factory t5 on t1.FactoryID=t5.ID "
					+ " left join {0}.wms_storeregion t2 on t1.StoreRegionID=t2.ID "
					+ " left join {0}.mbs_user t3 on t.CreatorID=t3.ID "
					+ " left join {0}.mbs_user t4 on t.EditorID=t4.ID "
					+ " left join {0}.mss_material t6 on t.MaterialID=t6.ID "
					+ " left join {0}.mss_unit t7 on t6.CYUnitID=t7.ID "
					+ " where (:ID<=0 or  t.ID=:ID) and (:Code =''''  or  t.Code=:Code)"
					+ " AND (:FactoryID<=0 or  t1.FactoryID=:FactoryID) "
					+ " AND (:StoreRegionID<=0 or  t1.StoreRegionID=:StoreRegionID) "
					+ " AND (:StoreAreaID< 0 or  t.StoreAreaID=:StoreAreaID) "
					+ " AND (:StoreID<=0 or  t.StoreID=:StoreID) AND (:MaterialID<=0 or  t.MaterialID=:MaterialID)"
					+ " AND (:Active<0 or  t.Active=:Active) and (:MaterialCode =''''  or  t6.Code=:MaterialCode)"
					+ " AND (:MaterialName = '''' or  t6.Name like :MaterialName) "
					+ " AND (:StoreName = '''' or  t1.Name like :StoreName) "
					+ " AND (:StoreCode = '''' or  t1.Code like :StoreCode) "
					+ " AND (:StoreAreaName = '''' or  t8.Name like :StoreAreaName) "
					+ " AND (:StoreAreaCode = '''' or  t8.Code like :StoreAreaCode) "
					+ " AND (:Name = '''' or  t.Name like :Name)", super.getDBName());

			wSQLText = this.DMLChange(wSQLText);

			wParwms.put("ID", wID);
			wParwms.put("Code", wCode);
			wParwms.put("FactoryID", wFactoryID);
			wParwms.put("StoreRegionID", wStoreRegionID);
			wParwms.put("StoreID", wStoreID);
			wParwms.put("StoreName", StringUtils.isEmpty(wStoreName) ? "" : ("%" + wStoreName + "%"));
			wParwms.put("StoreCode", StringUtils.isEmpty(wStoreCode) ? "" : ("%" + wStoreCode + "%"));
			wParwms.put("StoreAreaID", wStoreAreaID);
			wParwms.put("StoreAreaName", StringUtils.isEmpty(wStoreAreaName) ? "" : ("%" + wStoreAreaName + "%"));
			wParwms.put("StoreAreaCode", StringUtils.isEmpty(wStoreAreaCode) ? "" : ("%" + wStoreAreaCode + "%"));
			wParwms.put("MaterialID", wMaterialID);
			wParwms.put("MaterialCode", wMaterialCode);
			wParwms.put("MaterialName", StringUtils.isEmpty(wMaterialName) ? "" : ("%" + wMaterialName + "%"));
			wParwms.put("Active", wActive);
			wParwms.put("Name", StringUtils.isEmpty(wName) ? "" : ("%" + wName + "%"));

			wWMSStockSafeList = super.QueryForList(wSQLText, wParwms, wPagination, WMSStockSafe.class);

			// Step03:配置系统
		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("WMS_QueryStockSafeList", ex);
		}
		return wWMSStockSafeList;
	}

	public WMSStockSafe WMS_QueryStockSafe(BMSEmployee wLoginUser, int wID, String wCode,
			OutResult<Integer> wErrorCode) {
		WMSStockSafe wResult = new WMSStockSafe();
		wErrorCode.set(0);

		try {
			List<WMSStockSafe> wList = null;
			if (wID > 0) {
				wList = this.WMS_QueryStockSafeList(wLoginUser, wID, "", -1, -1, -1, "", "", -1, "", "", -1, "", "", "",
						-1, Pagination.getDefault(), wErrorCode);

			} else if (StringUtils.isNotEmpty(wCode)) {
				wList = this.WMS_QueryStockSafeList(wLoginUser, -1, wCode, -1, -1, -1, "", "", -1, "", "", -1, "", "",
						"", -1, Pagination.getDefault(), wErrorCode);
			}
			if (wList != null && wList.size() > 0) {
				wResult = wList.get(0);
			}
		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("WMS_QueryStockSafeByID", ex);
		}
		return wResult;
	}

	public WMSStockSafe WMS_QueryStockSafe(BMSEmployee wLoginUser, int wMaterialID, String wMaterialCode, int wStoreID,
			int wStoreAreaID, OutResult<Integer> wErrorCode) {
		WMSStockSafe wResult = new WMSStockSafe();
		wErrorCode.set(0);

		try {

			if (StringUtils.isEmpty(wMaterialCode) && wMaterialID <= 0) {
				return wResult;
			}
			if (wStoreID <= 0 && wStoreAreaID <= 0) {
				return wResult;
			}

			List<WMSStockSafe> wList = this.WMS_QueryStockSafeList(wLoginUser, -1, -1, wStoreID, "", "", wStoreAreaID,
					"", "", wMaterialID, wMaterialCode, "", "", -1, Pagination.getDefault(), wErrorCode);

			if (wList != null && wList.size() > 0) {
				wResult = wList.get(0);
			}
		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("WMS_QueryStockSafe", ex);
		}
		return wResult;
	}

	public WMSStockSafe WMS_CheckStockSafe(BMSEmployee wLoginUser, WMSStockSafe wWMSStockSafe,
			OutResult<Integer> wErrorCode) {
		WMSStockSafe wStockSafe = new WMSStockSafe();
		wErrorCode.set(0);

		try {

			// Step0:查询
			Map<String, Object> wParwms = new HashMap<String, Object>();
			String wSQLText = StringUtils.Format(
					"Select * from {0}.wms_stocksafe "
							+ " where ID!=:ID and  (StoreID=:StoreID  AND StoreAreaID=:StoreAreaID AND MaterialID=:MaterialID ) ",
					super.getDBName());
			wParwms.clear();
			wParwms.put("ID", wWMSStockSafe.ID);
			wParwms.put("MaterialID", wWMSStockSafe.MaterialID);
			wParwms.put("StoreID", wWMSStockSafe.StoreID);
			wParwms.put("StoreAreaID", wWMSStockSafe.StoreAreaID);

			wSQLText = this.DMLChange(wSQLText);
			List<Map<String, Object>> wQueryResultList = super.queryForList(wSQLText, wParwms);
			for (Map<String, Object> wSqlDataReader : wQueryResultList) {
				wStockSafe.ID = StringUtils.parseInt(wSqlDataReader.get("ID"));
				wStockSafe.MaterialID = StringUtils.parseInt(wSqlDataReader.get("MaterialID"));
				wStockSafe.StoreID = StringUtils.parseInt(wSqlDataReader.get("StoreID"));

				wStockSafe.CreatorID = StringUtils.parseInt(wSqlDataReader.get("CreatorID"));
				wStockSafe.CreateTime = StringUtils.parseCalendar(wSqlDataReader.get("CreateTime"));
				wStockSafe.EditorID = StringUtils.parseInt(wSqlDataReader.get("EditorID"));
				wStockSafe.EditTime = StringUtils.parseCalendar(wSqlDataReader.get("EditTime"));
				wStockSafe.Active = StringUtils.parseInt(wSqlDataReader.get("Active"));

			}

		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("WMS_CheckStockSafe", ex);
		}
		return wStockSafe;
	}

}
